package cn.datax.service.system.controller;

import cn.datax.common.base.BaseController;
import cn.datax.common.core.R;
import cn.datax.common.validate.ValidationGroups;
import cn.datax.service.system.api.dto.NoticeDto;
import cn.datax.service.system.api.entity.NoticeEntity;
import cn.datax.service.system.api.query.NoticeQuery;
import cn.datax.service.system.api.vo.NoticeVo;
import cn.datax.service.system.mapstruct.NoticeMapper;
import cn.datax.service.system.service.NoticeService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jimmy
 * @since 2022-07-30
 */
@Api(value="消息接口", tags = {"消息管理"})
@RestController
@RequestMapping("/notice")
public class NoticeController extends BaseController {
    @Autowired
    private NoticeService noticeService;

    @Autowired
    private NoticeMapper noticeMapper;

    @ApiOperation(value = "获取消息列表", notes = "")
    @GetMapping("/list")
    public R getNoticeList(NoticeQuery noticeQuery) {
        QueryWrapper<NoticeEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(noticeQuery.getNoticeTitle()), "notice_title", noticeQuery.getNoticeTitle());
        queryWrapper.eq(StrUtil.isNotBlank(noticeQuery.getNoticeType()), "notice_type", noticeQuery.getNoticeType());
        List<NoticeEntity> list = noticeService.list(queryWrapper);
        List<NoticeVo> collect = list.stream().map(noticeMapper::toVO).collect(Collectors.toList());
        return R.ok().setData(collect);
    }


    @ApiOperation(value = "获取消息详细信息", notes = "根据url的id来获取消息详细信息")
    @ApiImplicitParam(name = "id", value = "消息ID", required = true, dataType = "String", paramType = "path")
    @GetMapping("/{id}")
    public R getDeptById(@PathVariable String id) {
        NoticeEntity noticeEntity = noticeService.getById(id);
        return R.ok().setData(noticeMapper.toVO(noticeEntity));
    }


    @PutMapping("/{id}")
    public R updateDept(@PathVariable String id, @RequestBody @Validated({ValidationGroups.Update.class}) NoticeDto noticeDto) {
        NoticeEntity noticeEntity = noticeService.updateNotice(noticeDto);
        return R.ok().setData(noticeMapper.toVO(noticeEntity));
    }

    @ApiOperation(value = "删除消息", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(name = "id", value = "消息ID", required = true, dataType = "String", paramType = "path")
    @DeleteMapping("/{id}")
    public R deleteDept(@PathVariable String id) {
        noticeService.deleteNoticeById(id);
        return R.ok();
    }


    @ApiOperation(value = "创建消息", notes = "根据对象创建消息")
    @ApiImplicitParam(name = "notice", value = "消息详细实体notice", required = true, dataType = "NoticeDto")
    @PostMapping()
    public R saveNotice(@RequestBody @Validated({ValidationGroups.Insert.class}) NoticeDto noticeDto) {
        NoticeEntity noticeEntity = noticeService.saveNotice(noticeDto);
        return R.ok().setData(noticeMapper.toVO(noticeEntity));
    }

}
